package com.shadt.cloud.controller;

import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shadt.cloud.service.BranchService;
import com.shadt.cloud.vo.BranchVo;
import com.shadt.core.controller.BaseController;
import com.shadt.core.model.Json;
import com.shadt.sms.service.SendMsg;

/**
 * 分平台控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/branch")
public class BranchController extends BaseController {

	@Autowired
	SendMsg msg;
	
	@Autowired
	BranchService service;

	/**
	 * 发送短信验证码
	 * @param phone
	 * @return
	 */
	@ResponseBody	
	@RequestMapping(value="/send",method={RequestMethod.GET})
	public Json sendMsg(String phone){
		Json j = new Json();
		if("".equals(phone)||null==phone){
			j.setMsg("手机号码不能为空！");
			j.setSuccess(false);
		}else{
			try {
				j = msg.send(phone);
				final String p = phone;
				final String code =j.getObj().toString();
				new Timer().schedule(new TimerTask(){
					public void run() {
						msg.setStatus(p, code);
					}
				}, 1*60*1000);// 设定指定的时间,此处为15分钟
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return j;
	}
	
	
	/**
	 * 判断验证码是否正确
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/checkcode",method={RequestMethod.GET,RequestMethod.POST})
	public Json checkCode(String phone,String code){
		Json j = new Json();
		boolean check = false;
		try {
			check = msg.check(phone, code);
		} catch (Exception e) {
			e.printStackTrace();
		}
		j.setSuccess(check);
		return j;
	}
	
	/**
	 * 验证分平台是否已注册 
	 * @param appname
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/checkbranch",method={RequestMethod.GET})
	public Json checkBranch(String appname){
		Json j = new Json();
		BranchVo vo =null;
		try {
			vo = service.checkBranch(appname);
			//vo.setSecret(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(vo!=null){
			j.setSuccess(false);
			j.setObj(vo);
			j.setMsg("该平台已注册！");
		}else{
			j.setSuccess(true);
			j.setMsg("该平台可以注册！");
		}
		return j;
	}
	
	
	/**
	 * 分平台注册
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/register", method = {RequestMethod.GET, RequestMethod.POST})
	public Json register(String appname, String username, String phone,String password) {
		Json j = new Json();
		BranchVo vo = null;
		try {
			vo = service.register(appname, username, phone,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if("".equals(vo.getSecret())||null==vo.getSecret()){
			j.setSuccess(false);
			j.setMsg("注册失败,该平台已申请云平台数据获取权限！");
		}else{
			j.setMsg("分平台注册成功！");
			j.setSuccess(true);
		}
		j.setObj(vo);
		return j;
	}
}
